package com.ryskoo.catering.ordermain.repository;

import com.ryskoo.catering.constant.HCLCacheConstant;
import com.ryskoo.catering.ordermain.domain.OrderMainPO;
import com.ryskoo.common.dto.customer.VendorIndexInfo;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author BeHappy
 */
@Primary
@Repository
public class OrderMainRedisImpl extends BasicRedisImpl<OrderMainPO> implements IOrderMainRepository{
    private static final String cacheKey = HCLCacheConstant.HCL_ORDERMAIN_KEY;

    @Autowired
    private RedisUtil redisClient;
    @Autowired
    private IOrderMainRepository orderMainRepository;

    @PostConstruct
    public void init() {
        this.setRedisClient(redisClient);
        this.setDao(orderMainRepository);
    }

    public OrderMainRedisImpl() {
        super(cacheKey);
    }

    @Override
    public void create(OrderMainPO orderMainPO) {
        super.create(orderMainPO);
        redisClient.setObject(cacheKey + orderMainPO.getEntityNo(), orderMainPO);
    }

    @Override
    public void update(OrderMainPO orderMainPO) {
        super.update(orderMainPO);
        redisClient.setObject(cacheKey + orderMainPO.getEntityNo(), orderMainPO);
    }

    @Override
    public void delete(OrderMainPO orderMainPO) {
        super.delete(orderMainPO);
        redisClient.remove(cacheKey + orderMainPO.getEntityNo());
    }

    @Override
    public void deletes(List<String> needDeleteUuids) {
        super.deletes(needDeleteUuids);
        for (String deleteUuid : needDeleteUuids) {
            redisClient.remove(cacheKey + deleteUuid);
        }
    }

    /**
     * 根据订单编号获取订单
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderMainPO getByOrderId(String orderId) {
        OrderMainPO po = (OrderMainPO) redisClient.getObject(cacheKey + orderId, OrderMainPO.class);
        if(null != po){
            return po;
        }
        po = orderMainRepository.getByOrderId(orderId);
        if(null == po){
            return null;
        }
        redisClient.setObject(cacheKey + orderId, po);
        return po;
    }

    @Override
    public int getOrderCountByDate(String date, String vendorUuid) {
        return orderMainRepository.getOrderCountByDate(date,vendorUuid);
    }

    @Override
    public double getOrderAmtByDate(String date, String vendorUuid) {
        return orderMainRepository.getOrderAmtByDate(date,vendorUuid);
    }

    @Override
    public VendorIndexInfo getDailyVendorIndexInfo(String vendorUuid, String date) {
    	VendorIndexInfo indexInfo = null;
    	
    	if (!StringUtils.isEmpty(date)) {
    		indexInfo = (VendorIndexInfo) redisClient.getObject(vendorUuid + "_" + date);
    		if (null == indexInfo) {
    			indexInfo = orderMainRepository.getDailyVendorIndexInfo(vendorUuid, date);
    		}
    	} else {
    		indexInfo = orderMainRepository.getDailyVendorIndexInfo(vendorUuid, date);
    	}
    	
    	if (null != indexInfo) {
    		 indexInfo.setVendorUuid(vendorUuid);
             indexInfo.setDate(date);
             indexInfo.setIncome(null != indexInfo.getIncome() ? indexInfo.getIncome() : 0.0);
             indexInfo.setQuantity(null != indexInfo.getQuantity() ? indexInfo.getQuantity() : 0);
             indexInfo.setPeople(null != indexInfo.getPeople() ? indexInfo.getPeople() : 0);
    	}
    	
    	if (!StringUtils.isEmpty(date)) {
    		 redisClient.setExpireObject(vendorUuid + "_" + date, indexInfo, 8L, TimeUnit.DAYS);
    	}

        return indexInfo;
    }

    /**
     * 查询商品月销数量
     *
     * @param products
     * @param todayDateStr
     * @param monthBeforeDateStr
     * @return
     */
    @Override
    public List<Object[]> getMonthSellNum(List<String> products, String todayDateStr, String monthBeforeDateStr) {
        return orderMainRepository.getMonthSellNum(products, todayDateStr, monthBeforeDateStr);
    }

    /**
     * 查询1个月内商户月销商品数量
     *
     * @param vendorUuid
     * @param todayDateStr
     * @param monthBeforeDateStr
     * @return
     */
    @Override
    public int getMonthSellProductNum(String vendorUuid, String todayDateStr, String monthBeforeDateStr) {
        return orderMainRepository.getMonthSellProductNum(vendorUuid, todayDateStr, monthBeforeDateStr);
    }
}
